package com.qixin.modules.rzInvoice.service.impl;

import com.qixin.modules.rzInvoice.entity.EaRzDdcxxcd;
import com.qixin.modules.rzInvoice.entity.EaRzDdcxxcdItems;
import com.qixin.modules.rzInvoice.mapper.EaRzDdcxxcdItemsMapper;
import com.qixin.modules.rzInvoice.mapper.EaRzDdcxxcdMapper;
import com.qixin.modules.rzInvoice.service.IEaRzDdcxxcdService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 滴滴出行行程单
 * @Author: qixin-boot
 * @Date:   2021-07-08
 * @Version: V1.0
 */
@Service
public class EaRzDdcxxcdServiceImpl extends ServiceImpl<EaRzDdcxxcdMapper, EaRzDdcxxcd> implements IEaRzDdcxxcdService {

	@Autowired
	private EaRzDdcxxcdMapper eaRzDdcxxcdMapper;
	@Autowired
	private EaRzDdcxxcdItemsMapper eaRzDdcxxcdItemsMapper;
	
	@Override
	@Transactional
	public void saveMain(EaRzDdcxxcd eaRzDdcxxcd, List<EaRzDdcxxcdItems> eaRzDdcxxcdItemsList) {
		eaRzDdcxxcdMapper.insert(eaRzDdcxxcd);
		if(eaRzDdcxxcdItemsList!=null && eaRzDdcxxcdItemsList.size()>0) {
			for(EaRzDdcxxcdItems entity:eaRzDdcxxcdItemsList) {
				//外键设置
				entity.setRzDdcxxcdId(eaRzDdcxxcd.getId());
				eaRzDdcxxcdItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaRzDdcxxcd eaRzDdcxxcd,List<EaRzDdcxxcdItems> eaRzDdcxxcdItemsList) {
		eaRzDdcxxcdMapper.updateById(eaRzDdcxxcd);
		
		//1.先删除子表数据
		eaRzDdcxxcdItemsMapper.deleteByMainId(eaRzDdcxxcd.getId());
		
		//2.子表数据重新插入
		if(eaRzDdcxxcdItemsList!=null && eaRzDdcxxcdItemsList.size()>0) {
			for(EaRzDdcxxcdItems entity:eaRzDdcxxcdItemsList) {
				//外键设置
				entity.setRzDdcxxcdId(eaRzDdcxxcd.getId());
				eaRzDdcxxcdItemsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaRzDdcxxcdItemsMapper.deleteByMainId(id);
		eaRzDdcxxcdMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaRzDdcxxcdItemsMapper.deleteByMainId(id.toString());
			eaRzDdcxxcdMapper.deleteById(id);
		}
	}
	
}
